using SS;
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace CustomListView
{
	public class ListViewEx : ListView
	{
		internal struct RECT
		{
			public int left;
			public int top;
			public int right;
			public int bottom;
		}
		internal class Win32
		{
			public const int LVM_GETSUBITEMRECT = 4152;
			public const int LVIR_BOUNDS = 0;
			[DllImport("user32.dll", SetLastError = true)]
			public static extern int SendMessage(IntPtr hWnd, int messageID, int wParam, ref ListViewEx.RECT lParam);
		}
		internal class SubItem
		{
			public readonly int row;
			public readonly int col;
			public SubItem(int row, int col)
			{
				this.row = row;
				this.col = col;
			}
		}
		private TextBox txt;
		private bool addSubItem = false;
		private bool hideComboAfterSelChange = false;
		private int row = -1;
		private int col = -1;
		private TextBox textBox = new TextBox();
		private ComboBox combo = new ComboBox();
		private bool mouseDown = false;
		private Hashtable customCells = new Hashtable();
		public string typeOfListView = "";
		public TextBox txtTextBox
		{
			get
			{
				return this.txt;
			}
			set
			{
				this.txt = value;
			}
		}
		public bool AddSubItem
		{
			set
			{
				this.addSubItem = value;
			}
		}
		public bool HideComboAfterSelChange
		{
			set
			{
				this.hideComboAfterSelChange = value;
			}
		}
		public ListViewEx()
		{
			this.InitializeComponent();
		}
		public ListViewEx(TextBox txt)
		{
			this.txt = txt;
			this.InitializeComponent();
		}
		private void InitializeComponent()
		{
			this.textBox.Visible = false;
			this.textBox.BorderStyle = BorderStyle.FixedSingle;
			this.textBox.Leave += new EventHandler(this.textBox_Leave);
			this.textBox.KeyPress += new KeyPressEventHandler(this.textBox_KeyPress);
			this.combo.FlatStyle = FlatStyle.Flat;
			this.combo.Visible = false;
			base.Controls.Add(this.textBox);
			base.Controls.Add(this.combo);
			this.combo.DropDownStyle = ComboBoxStyle.DropDownList;
			this.combo.SelectedIndexChanged += new EventHandler(this.combo_SelectedIndexChanged);
		}
		private ListViewEx.RECT GetSubItemRect(Point clickPoint)
		{
			ListViewEx.RECT result = default(ListViewEx.RECT);
			this.row = (this.col = -1);
			ListViewItem itemAt = base.GetItemAt(clickPoint.X, clickPoint.Y);
			if (itemAt != null)
			{
				for (int i = 0; i < base.Columns.Count; i++)
				{
					result.top = i + 1;
					result.left = 0;
					try
					{
						int num = ListViewEx.Win32.SendMessage(base.Handle, 4152, itemAt.Index, ref result);
						if (num == 0)
						{
							throw new Win32Exception();
						}
						if (clickPoint.X < result.left)
						{
							this.row = itemAt.Index;
							this.col = 0;
							break;
						}
						if (clickPoint.X >= result.left & clickPoint.X <= result.right)
						{
							this.row = itemAt.Index;
							this.col = i + 1;
							break;
						}
					}
					catch (Win32Exception ex)
					{
						Trace.WriteLine(string.Format("Exception while getting subitem rect, {0}", ex.Message));
					}
				}
			}
			return result;
		}
		public void AddEditableCell(int row, int col)
		{
			this.customCells[new ListViewEx.SubItem(row, col)] = null;
		}
		public void AddComboBoxCell(int row, int col, StringCollection data)
		{
			this.customCells[new ListViewEx.SubItem(row, col)] = data;
		}
		public void AddComboBoxCell(int row, int col, string[] data)
		{
			try
			{
				StringCollection stringCollection = new StringCollection();
				stringCollection.AddRange(data);
				this.AddComboBoxCell(row, col, stringCollection);
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.ToString());
			}
		}
		private void ShowComboBox(Point location, Size sz, StringCollection data)
		{
			try
			{
				this.combo.Size = sz;
				this.combo.Location = location;
				this.combo.DropDownStyle = ComboBoxStyle.DropDownList;
				this.combo.Items.Clear();
				foreach (string current in data)
				{
					this.combo.Items.Add(current);
				}
				this.combo.Text = base.Items[this.row].SubItems[this.col].Text;
				this.combo.DropDownWidth = this.GetDropDownWidth(data);
				this.combo.Show();
			}
			catch (ArgumentOutOfRangeException)
			{
			}
		}
		private void ShowTextBox(Point location, Size sz)
		{
			try
			{
				this.textBox.Size = sz;
				this.textBox.Location = location;
				this.textBox.Text = base.Items[this.row].SubItems[this.col].Text;
				this.textBox.Show();
				this.textBox.Focus();
			}
			catch (ArgumentOutOfRangeException)
			{
			}
		}
		protected override void OnMouseUp(MouseEventArgs e)
		{
			try
			{
				this.textBox.Visible = (this.combo.Visible = false);
				if (this.mouseDown)
				{
					if (base.FullRowSelect && base.View == View.Details)
					{
						this.mouseDown = false;
						ListViewEx.RECT subItemRect = this.GetSubItemRect(new Point(e.X, e.Y));
						if (this.row != -1 && this.col != -1)
						{
							ListViewEx.SubItem key = this.GetKey(new ListViewEx.SubItem(this.row, this.col));
							if (key != null)
							{
								Size sz = new Size(base.Columns[this.col].Width, base.Items[this.row].Bounds.Height);
								Point location = (this.col == 0) ? new Point(0, subItemRect.top) : new Point(subItemRect.left, subItemRect.top);
								this.ValidateAndAddSubItems();
								if (this.customCells[key] == null)
								{
									this.ShowTextBox(location, sz);
								}
								else
								{
									this.ShowComboBox(location, sz, (StringCollection)this.customCells[key]);
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.ToString());
			}
		}
		private void ValidateAndAddSubItems()
		{
			try
			{
				while (base.Items[this.row].SubItems.Count < base.Columns.Count && this.addSubItem)
				{
					base.Items[this.row].SubItems.Add("");
				}
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.ToString());
			}
		}
		private int GetDropDownWidth(StringCollection data)
		{
			int result;
			if (data.Count == 0)
			{
				result = this.combo.Width;
			}
			else
			{
				string text = data[0];
				foreach (string current in data)
				{
					if (text.Length < current.Length)
					{
						text = current;
					}
				}
				result = (int)base.CreateGraphics().MeasureString(text, this.Font).Width;
			}
			return result;
		}
		private ListViewEx.SubItem GetKey(ListViewEx.SubItem cell)
		{
			ListViewEx.SubItem result;
			try
			{
				foreach (ListViewEx.SubItem subItem in this.customCells.Keys)
				{
					if (subItem.row == cell.row && subItem.col == cell.col)
					{
						result = subItem;
						return result;
					}
					if (subItem.row == -1 && subItem.col == cell.col)
					{
						result = subItem;
						return result;
					}
					if (subItem.row == cell.row && subItem.col == -1)
					{
						result = subItem;
						return result;
					}
					if (subItem.row == -1 && subItem.col == -1)
					{
						result = subItem;
						return result;
					}
				}
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.ToString());
			}
			result = null;
			return result;
		}
		protected override void OnMouseDown(MouseEventArgs e)
		{
			try
			{
				this.mouseDown = true;
				this.textBox.Hide();
				this.combo.Hide();
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.ToString());
			}
		}
		private void textBox_Leave(object sender, EventArgs e)
		{
			try
			{
				if (this.row != -1 && this.col != -1 && this.textBox.Text.Trim() != "")
				{
					if (this.typeOfListView == "nhap")
					{
						if (this.col != 2)
						{
							if (this.checkValue(this.textBox.Text.Replace("%", "").Trim(), 8, this.col))
							{
								base.Items[this.row].SubItems[this.col].Text = this.textBox.Text.Replace("%", "").Trim();
								this.textBox.Hide();
								this.UpdateValue(this.row);
							}
							else
							{
								this.textBox.Hide();
							}
							this.txtTextBox.Text = string.Format(Program.lenAffterDecimal, this.CheckMoney());
						}
						else
						{
							base.Items[this.row].SubItems[this.col].Text = this.textBox.Text.Trim();
							this.textBox.Hide();
						}
					}
					else
					{
						if (this.typeOfListView == "capnhaptondau")
						{
							if (this.checkValue(this.textBox.Text.Trim()) && double.Parse(base.Items[this.row].SubItems[this.col].Text.Trim().Replace(",", "").Replace(".", "")) != double.Parse(this.textBox.Text.Trim().Replace(",", "").Replace(".", "")))
							{
								base.Items[this.row].SubItems[this.col].Text = this.textBox.Text.Trim();
								this.textBox.Hide();
								base.Items[this.row].SubItems[8].Text = "1";
							}
							else
							{
								this.textBox.Hide();
							}
						}
						else
						{
							base.Items[this.row].SubItems[this.col].Text = this.textBox.Text.Trim();
							this.textBox.Hide();
						}
					}
				}
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.ToString());
			}
		}
		private double CheckMoney()
		{
			double num = 0.0;
			try
			{
				if (base.Items.Count > 0)
				{
					for (int i = 0; i < base.Items.Count; i++)
					{
						num += double.Parse(base.Items[i].SubItems[9].Text.Replace(".", "").Replace(",", ""));
					}
				}
			}
			catch
			{
			}
			return num;
		}
		private void textBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			try
			{
				if (e.KeyChar == '\r' && this.textBox.Text.Trim() != "")
				{
					if (this.row != -1 && this.col != -1)
					{
						if (this.typeOfListView == "nhap")
						{
							if (this.col != 2)
							{
								if (this.checkValue(this.textBox.Text.Replace("%", "").Trim(), 8, this.col))
								{
									base.Items[this.row].SubItems[this.col].Text = this.textBox.Text.Replace("%", "").Trim();
									this.textBox.Hide();
									this.UpdateValue(this.row);
								}
								else
								{
									this.textBox.Hide();
								}
								this.txtTextBox.Text = string.Format(Program.lenAffterDecimal, this.CheckMoney());
							}
							else
							{
								base.Items[this.row].SubItems[this.col].Text = this.textBox.Text.Trim();
								this.textBox.Hide();
							}
						}
						else
						{
							if (this.typeOfListView == "capnhaptondau")
							{
								if (this.checkValue(this.textBox.Text.Trim()) && double.Parse(base.Items[this.row].SubItems[this.col].Text.Trim().Replace(",", "").Replace(".", "")) != double.Parse(this.textBox.Text.Trim().Replace(",", "").Replace(".", "")))
								{
									base.Items[this.row].SubItems[this.col].Text = this.textBox.Text.Trim();
									this.textBox.Hide();
									base.Items[this.row].SubItems[8].Text = "1";
								}
								else
								{
									this.textBox.Hide();
								}
							}
							else
							{
								base.Items[this.row].SubItems[this.col].Text = this.textBox.Text.Trim();
								this.textBox.Hide();
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.ToString());
			}
		}
		private void combo_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				if (this.row != -1 && this.col != -1)
				{
					base.Items[this.row].SubItems[this.col].Text = this.combo.Text;
					this.combo.Visible = !this.hideComboAfterSelChange;
				}
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.ToString());
			}
		}
		private void UpdateValue(int tmprow)
		{
			if (this.typeOfListView == "nhap")
			{
				int num = int.Parse(base.Items[tmprow].SubItems[12].Text);
				double num2 = double.Parse(base.Items[tmprow].SubItems[4].Text.Trim());
				double num3 = double.Parse(base.Items[tmprow].SubItems[5].Text.Trim());
				double num4 = double.Parse(base.Items[tmprow].SubItems[7].Text.Trim());
				double num5 = double.Parse(base.Items[tmprow].SubItems[8].Text.Replace("%", "").Trim());
				double num6 = num2 + num3 / (double)num;
				base.Items[tmprow].SubItems[6].Text = num6.ToString();
				double num7 = num6 * num4 * (100.0 - num5) / 100.0;
				base.Items[tmprow].SubItems[9].Text = string.Format(Program.lenAffterDecimal, num7);
				base.Items[tmprow].SubItems[8].Text = num5.ToString() + "%";
				double num8 = num2 * (double)num + num3;
				base.Items[tmprow].SubItems[11].Text = num8.ToString();
			}
		}
		private bool checkValue(string tmpText)
		{
			bool result;
			try
			{
				double num = double.Parse(tmpText.Replace(",", "").Replace(".", ""));
			}
			catch
			{
				result = false;
				return result;
			}
			result = true;
			return result;
		}
		private bool checkValue(string tmpText, int tmpRowCkToCheck, int tmpThisCol)
		{
			bool result;
			try
			{
				double num = double.Parse(tmpText);
				if (tmpThisCol == tmpRowCkToCheck)
				{
					if (num > 100.0 || num < 0.0)
					{
						result = false;
						return result;
					}
				}
			}
			catch
			{
				result = false;
				return result;
			}
			result = true;
			return result;
		}
	}
}
